package cn.iocoder.yudao.module.pay.splitting.controller.admin;

import cn.iocoder.yudao.framework.business.basic.annotation.AdminJsonView;
import cn.iocoder.yudao.framework.business.basic.controller.PageController;
import cn.iocoder.yudao.framework.business.basic.controller.SimpleDetailController;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.member.agency.AgencyUserApi;
import cn.iocoder.yudao.module.api.member.agency.vo.AgencyApplyVO;
import cn.iocoder.yudao.module.api.member.eid.MemberEidApi;
import cn.iocoder.yudao.module.api.member.eid.vo.EidVO;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.pay.splitting.pojo.dto.SplittingMerchantQueryDTO;
import cn.iocoder.yudao.module.pay.splitting.pojo.dto.SplittingWithdrawDTO;
import cn.iocoder.yudao.module.pay.splitting.pojo.vo.MallBookBalanceVO;
import cn.iocoder.yudao.module.pay.splitting.pojo.vo.SplittingMerchantVO;
import cn.iocoder.yudao.module.pay.splitting.service.admin.SplittingMerchantService;
import cn.iocoder.yudao.module.pay.splitting.service.core.CorePaySplittingService;
import cn.iocoder.yudao.module.pay.splitting.service.core.CoreSplittingMerchantService;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 前端控制器
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/05/17
 */
@Tag(name = "管理后台 - 分账商户")
@RestController
@RequestMapping("/pay/splitting/merchant")
public class SplittingMerchantController implements SimpleDetailController<SplittingMerchantVO>, PageController<SplittingMerchantVO, SplittingMerchantQueryDTO> {
    @Resource
    private SplittingMerchantService splittingMerchantService;
    @Resource
    private CoreSplittingMerchantService coreSplittingMerchantService;
    @Resource
    private CorePaySplittingService corePaySplittingService;

    @Resource
    private MemberUserApi memberUserApi;
    @Resource
    private MemberEidApi memberEidApi;
    @Resource
    private ShopInfoApi shopInfoApi;
    @Resource
    private AgencyUserApi agencyUserApi;

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @JsonView(AdminJsonView.class)
    @Override
    public CommonResult<SplittingMerchantVO> getDetail(Long id) {
        return CommonResult.success(splittingMerchantService.getDetail(id));
    }

    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    @JsonView(AdminJsonView.class)
    @Override
    public CommonResult<PageResult<SplittingMerchantVO>> getPage(Integer pageNo, Integer pageSize, SplittingMerchantQueryDTO param) {
        return CommonResult.success(splittingMerchantService.getPage(pageNo, pageSize, param));
    }

    @PostMapping("/sync-user")
    @Operation(summary = "同步商户信息（普通用户）")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1")
    @JsonView(AdminJsonView.class)
    @PreAuthorize("@ss.hasAnyRoles('super_admin')")
    public CommonResult<SplittingMerchantVO> syncUserInfo(@RequestParam("userId") Long userId) {
        EidVO eidInfo = memberEidApi.getEidInfo(userId);
        Assertions.isTrue(eidInfo != null, "实名信息不存在");
        String mobile = memberUserApi.getUserMobile(userId);
        SplittingMerchantVO vo = coreSplittingMerchantService.syncInfoIfNecessary(mobile, eidInfo.getRealName(), eidInfo.getIdcardNo());
        return CommonResult.success(vo);
    }

    @PostMapping("/sync-shop")
    @Operation(summary = "同步商户信息（商家）")
    @Parameter(name = "shopId", description = "店铺编号", required = true, example = "1")
    @JsonView(AdminJsonView.class)
    @PreAuthorize("@ss.hasAnyRoles('super_admin')")
    public CommonResult<SplittingMerchantVO> syncShopInfo(@RequestParam("shopId") Long shopId) {
        ShopInfoDTO shopInfo = shopInfoApi.getShopInfo(shopId);
        Assertions.isTrue(shopInfo != null, "店铺信息不存在");
        memberEidApi.updateEidInfo(shopInfo.getUserId(), shopInfo.getContactName(), shopInfo.getOwnerEidNo());
        SplittingMerchantVO vo = coreSplittingMerchantService.syncInfoIfNecessary(shopInfo.getContactMobile(), shopInfo.getContactName(), shopInfo.getOwnerEidNo());
        return CommonResult.success(vo);
    }

    @PostMapping("/sync-agency")
    @Operation(summary = "同步商户信息（招商人员）")
    @Parameter(name = "userId", description = "用户编号", required = true, example = "1")
    @JsonView(AdminJsonView.class)
    @PreAuthorize("@ss.hasAnyRoles('super_admin')")
    public CommonResult<SplittingMerchantVO> syncAgencyInfo(@RequestParam("userId") Long userId) {
        AgencyApplyVO applyInfo = agencyUserApi.getApplyInfo(userId);
        Assertions.isTrue(applyInfo != null, "招商人员申请信息不存在");
        memberEidApi.updateEidInfo(applyInfo.getUserId(), applyInfo.getContactName(), applyInfo.getOwnerEidNo());
        SplittingMerchantVO vo = coreSplittingMerchantService.syncInfoIfNecessary(applyInfo.getContactMobile(), applyInfo.getContactName(), applyInfo.getOwnerEidNo());
        return CommonResult.success(vo);
    }

    @GetMapping("/platform-balance")
    @Operation(summary = "平台商户号余额")
    @JsonView(AdminJsonView.class)
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<MallBookBalanceVO> queryPlatformBalance() {
        return CommonResult.success(corePaySplittingService.queryPlatformBalance());
    }

    @PostMapping("/platform-withdraw")
    @Operation(summary = "平台商户号提现")
    @JsonView(AdminJsonView.class)
    @PreAuthorize("@ss.hasAnyRoles('super_admin', 'platform_admin', 'tenant_admin')")
    public CommonResult<Boolean> withdrawPlatformBalance(@Valid @RequestBody SplittingWithdrawDTO param) {
        return CommonResult.success(corePaySplittingService.withdrawPlatformBalance(param.getAmount()));
    }

}
